package com.springboot.test.springbootcrud.dao;

import com.springboot.test.springbootcrud.entity.Department;
import com.springboot.test.springbootcrud.entity.Employee;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/12/16 19:59
 */
@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees = null;
    private static Integer initId = 1006;

    static {
        employees = new HashMap<>();
        employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA"), new Date()));
        employees.put(1002, new Employee(1002, "E-bb", "BB@163.com", 0, new Department(102, "D-BB"), new Date()));
        employees.put(1003, new Employee(1003, "E-cc", "cc@163.com", 1, new Department(103, "D-CC"), new Date()));
        employees.put(1004, new Employee(1004, "E-dd", "dd@163.com", 0, new Department(104, "D-DD"), new Date()));
        employees.put(1005, new Employee(1005, "E-ee", "ee@163.com", 1, new Department(105, "D-EE"), new Date()));
    }

    @Autowired
    private DepartmentDao departmentDao;

    public void save(Employee employee) {
        if (employee.getId() == null) {
            employee.setId(initId++);
        }

        employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }

    public List<Employee> getEmployess() {
        return new ArrayList<>(employees.values());
    }

    public void delEmp(Integer id) {
        employees.remove(id);
    }

    public Employee get(Integer id) {
        return employees.get(id);
    }
}
